package com.evan.review.architecture.components.navigation;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.evan.review.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页Fragment
 */
public class HomeFragment extends Fragment {
    
    private RecyclerView recyclerView;
    private ItemAdapter adapter;
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }
    
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // 初始化RecyclerView
        recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        
        // 创建并设置适配器
        adapter = new ItemAdapter();
        recyclerView.setAdapter(adapter);
        
        // 加载数据
        loadItems();
    }
    
    /**
     * 加载列表项
     */
    private void loadItems() {
        List<NavigationItem> items = new ArrayList<>();
        for (int i = 1; i <= 20; i++) {
            items.add(new NavigationItem(i, "项目 " + i, "这是项目 " + i + " 的描述"));
        }
        adapter.submitList(items);
    }
    
    /**
     * 列表项适配器
     */
    private class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
        
        private List<NavigationItem> items = new ArrayList<>();
        
        @NonNull
        @Override
        public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_navigation, parent, false);
            return new ItemViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
            NavigationItem item = items.get(position);
            holder.bind(item);
        }
        
        @Override
        public int getItemCount() {
            return items.size();
        }
        
        public void submitList(List<NavigationItem> items) {
            this.items = items;
            notifyDataSetChanged();
        }
        
        class ItemViewHolder extends RecyclerView.ViewHolder {
            private final TextView tvTitle;
            private final TextView tvDescription;
            private final Button btnViewDetail;
            private NavigationItem item;
            
            public ItemViewHolder(@NonNull View itemView) {
                super(itemView);
                tvTitle = itemView.findViewById(R.id.tv_title);
                tvDescription = itemView.findViewById(R.id.tv_description);
                btnViewDetail = itemView.findViewById(R.id.btn_view_detail);
                
                // 设置点击事件
                btnViewDetail.setOnClickListener(v -> {
                    if (item != null) {
                        // 使用Safe Args创建导航操作
                        HomeFragmentDirections.ActionHomeFragmentToDetailFragment action =
                                HomeFragmentDirections.actionHomeFragmentToDetailFragment();
                        action.setItemId(item.getId());
                        Navigation.findNavController(itemView).navigate(action);
                    }
                });
            }
            
            public void bind(NavigationItem item) {
                this.item = item;
                tvTitle.setText(item.getTitle());
                tvDescription.setText(item.getDescription());
            }
        }
    }
} 